package gdut.imis.controller;

import gdut.imis.Utils.Result;
import gdut.imis.domain.FileType;
import gdut.imis.domain.User;
import gdut.imis.mapper.UserMapper;
import gdut.imis.service.LoginService;
import gdut.imis.service.UserService;
import gdut.imis.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author JiaFu
 * @Date 2023/4/8 19:28
 */
@RestController
@RequestMapping("/user")
@Api(value = "用户接口", tags = {"用户接口"})
@RequiredArgsConstructor
public class UserController extends BaseController {
    private final UserMapper userMapper;
    private final UserService userService;
    private final LoginService loginService;

    @ApiOperation("新增用户")
    @PostMapping("/add")
    @ApiImplicitParams({@ApiImplicitParam(name = "birthday", value = "生日", dataType = "String")})
    public Result add(@RequestBody User user) {
        return userService.addUser(user);
    }

    @ApiOperation("获取用户信息")
    @GetMapping("/getUserInfo")
    public Result getUserInfo() {
        return userService.getUserInfo();
    }

    @ApiOperation("获取用户详细信息展示于个人主页")
    @GetMapping("/getUserDetails")
    public Result getUserDetails() {
        return userService.getUserDetails();
    }

    @ApiOperation("更新用户基本信息")
    @PutMapping("/updateUserInfo")
    public Result updateUserInfo(@RequestBody User user) {
        return userService.updateUserInfo(user);
    }

    @ApiOperation("用户登录")
    @GetMapping("/login")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "用户账号", dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码", dataType = "String"),
            @ApiImplicitParam(name = "flag", value = "是否为管理员,0 : 否; 1 : 是", dataType = "Int")})
    public Result login(String userName, String password, Integer flag) {
        Map<String, Object> map = loginService.login(userName, password, flag);
        return resturnDataList(map);
    }

    @ApiOperation("推出登录")
    @GetMapping("/logout")
    public Result logout() {
        return loginService.logOut();
    }

    @ApiOperation("用户修改密码（通用，包括管理员）")
    @GetMapping("/updatePassword")
    public Result updatePassword(@RequestParam("originPassword") String originPassword, @RequestParam("newPassword") String newPassword) {
        return userService.updatePassword(originPassword, newPassword);
    }

    @ApiOperation("测试")
    @GetMapping("/test")
    public Result test() {
        List<FileType> permissions = userMapper.getPermissions(1644666146860879873L);
        List<String> fileWritePermissions = permissions.stream().map(FileType::getType).collect(Collectors.toList());
        return Result.success(fileWritePermissions);
    }

    @ApiOperation("根据学校id查询该校老师")
    @GetMapping("getTeacherListByUniversityId/{universityId}")
    public Result getTeacherListByUniversityId(@PathVariable("universityId") Long universityId) {
        return userService.getTeacherListByUniversityId(universityId);
    }

    @ApiOperation("新增学生个人档案(要拥有users权限)")
    @PostMapping("/addStudent")
    @PreAuthorize("@cp.hasPermission('users')")
    public Result addStudent(@RequestBody UserVo userVo) {
        return userService.addStudent(userVo);
    }

    @ApiOperation("通过学生id获取学生档案回忆录")
    @GetMapping("/getFileMemoryByStudentId/{studentId}")
    public Result getFileMemoryByStudentId(@PathVariable("studentId") Long studentId) throws ParseException {
        return userService.getFileMemoryByStudentId(studentId);
    }

}
